library(dplyr)   
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(purrr)
library(tidyr)
library(broom)

library(ggbio)
## Loading required package: BiocGenerics
## Warning: package 'BiocGenerics' was built under R version 3.6.1
## Loading required package: parallel
## 
## Attaching package: 'BiocGenerics'
## The following objects are masked from 'package:parallel':
## 
##     clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
##     clusterExport, clusterMap, parApply, parCapply, parLapply,
##     parLapplyLB, parRapply, parSapply, parSapplyLB
## The following objects are masked from 'package:dplyr':
## 
##     combine, intersect, setdiff, union
## The following objects are masked from 'package:stats':
## 
##     IQR, mad, sd, var, xtabs
## The following objects are masked from 'package:base':
## 
##     anyDuplicated, append, as.data.frame, basename, cbind, colnames,
##     dirname, do.call, duplicated, eval, evalq, Filter, Find, get, grep,
##     grepl, intersect, is.unsorted, lapply, Map, mapply, match, mget,
##     order, paste, pmax, pmax.int, pmin, pmin.int, Position, rank,
##     rbind, Reduce, rownames, sapply, setdiff, sort, table, tapply,
##     union, unique, unsplit, which, which.max, which.min
## Loading required package: ggplot2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
## Need specific help about ggbio? try mailing 
##  the maintainer or visit http://tengfei.github.com/ggbio/
## 
## Attaching package: 'ggbio'
## The following objects are masked from 'package:ggplot2':
## 
##     geom_bar, geom_rect, geom_segment, ggsave, stat_bin, stat_identity,
##     xlim
library(ggfortify)
library(ggplot2)
library(glue) #string formatting in r
## 
## Attaching package: 'glue'
## The following object is masked from 'package:dplyr':
## 
##     collapse
library(HDtest) # covariance matrix comparison
library(data.table)
## 
## Attaching package: 'data.table'
## The following object is masked from 'package:purrr':
## 
##     transpose
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
set.seed(52)


library(remotes)
#remotes::install_github("CFWP/rags2ridges")
library(rags2ridges) # MLE normalise matrices

library(seewave) # rms function
## 
## Attaching package: 'seewave'
## The following object is masked from 'package:ggbio':
## 
##     rescale
library(broom) # pretty printing results

library(eegkit)
## Loading required package: eegkitdata
## Loading required package: bigsplines
## Loading required package: quadprog
## Loading required package: ica
## Warning: package 'ica' was built under R version 3.6.1
## Loading required package: rgl
## This build of rgl does not include OpenGL functions.  Use
##  rglwidget() to display results, e.g. via options(rgl.printRglwidget = TRUE).
## Loading required package: signal
## 
## Attaching package: 'signal'
## The following object is masked from 'package:seewave':
## 
##     unwrap
## The following object is masked from 'package:dplyr':
## 
##     filter
## The following objects are masked from 'package:stats':
## 
##     filter, poly
options(scipen=999)

df_segments1 <- read.csv2("~/Downloads/eeg_1/p1 versus p2/AH_P1_EXPERIMENT_1_Segmentation_multiplexed.csv.csv", header = F)
df_segments2 <- read.csv2("~/Downloads/eeg_1/p1 versus p2/AH_P1_EXPERIMENT_2_Segmentation_multiplexed.csv.csv", header = F)
df_segments3 <- read.csv2("~/Downloads/eeg_1/p1 versus p2/AH_P2_EXPERIMENT_1_Segmentation_multiplexed.csv.csv", header = F)
df_segments4 <- read.csv2("~/Downloads/eeg_1/p1 versus p2/AH_P2_EXPERIMENT_2_Segmentation_multiplexed.csv.csv", header = F)


# df_wavelet <- read.csv2("EEG Files.CSV/AH-p1-ex-1-wavelet -raw-multiplexed.csv/AH_P1_EXPERIMENT_1_Wavelets_Raw Data_multiplexed.csv.csv", header = F)
col_names <- c(
 'Fp1',
 'F3',
 'F7',
 'FT9',
 'FC5',
 'FC1',
 'C3',
 'T7',
 'TP9',
 'CP5',
 'CP1',
 'Pz',
 'P3',
 'P7',
 'O1',
 'Oz',
 'O2',
 'P4',
 'P8',
 'TP10',
 'CP6',
 'CP2',
 'Cz',
 'C4',
 'T8',
 'FT10',
 'FC6',
 'FC2',
 'F4',
 'F8',
 'Fp2')
colnames(df_segments1) <- col_names
colnames(df_segments2) <- col_names
colnames(df_segments3) <- col_names
colnames(df_segments4) <- col_names
df_segments1['trial_info'] <- 'A'
df_segments1['trial_block'] <- 'B'

df_segments1['trial_info'][1:3000, ] <- 'Trial 1'
df_segments1['trial_info'][3001:6000, ] <- 'Trial 2'
df_segments1['trial_info'][6001:9000, ] <- 'Trial 3'
df_segments1['trial_info'][9001:12000, ] <- 'Trial 4'
df_segments1['trial_info'][12001:15000, ] <- 'Trial 5'
df_segments1['trial_info'][15001:18000, ] <- 'Trial 6'
df_segments1['trial_info'][18001:21000, ] <- 'Trial 7'
df_segments1['trial_info'][21001:24000, ] <- 'Trial 8'
df_segments1['trial_info'][24001:27000, ] <- 'Trial 9'
df_segments1['trial_info'][27001:30000, ] <- 'Trial 10'
df_segments1['trial_info'][30001:33000, ] <- 'Trial 11'
df_segments1['trial_info'][33001:36000, ] <- 'Trial 12'
df_segments1['trial_info'][36001:39000, ] <- 'Trial 13'
df_segments1['trial_info'][39001:42000, ] <- 'Trial 14'
df_segments1['trial_info'][42001:45000, ] <- 'Trial 15'
df_segments1['trial_info'][45001:48000, ] <- 'Trial 16'
df_segments1['trial_info'][48001:51000, ] <- 'Trial 17'
df_segments1['trial_info'][51001:54000, ] <- 'Trial 18'
df_segments1['trial_info'][54001:57000, ] <- 'Trial 19'
df_segments1['trial_info'][57001:60000, ] <- 'Trial 20'

df_segments1['trial_block'][1:30000, ] <- 'Set 1'
df_segments1['trial_block'][30001:60000, ] <- 'Set 2'

df_segments2['trial_info'] <- 'A'
df_segments2['trial_block'] <- 'B'

df_segments2['trial_info'][1:3000, ] <- 'Trial 1'
df_segments2['trial_info'][3001:6000, ] <- 'Trial 2'
df_segments2['trial_info'][6001:9000, ] <- 'Trial 3'
df_segments2['trial_info'][9001:12000, ] <- 'Trial 4'
df_segments2['trial_info'][12001:15000, ] <- 'Trial 5'
df_segments2['trial_info'][15001:18000, ] <- 'Trial 6'
df_segments2['trial_info'][18001:21000, ] <- 'Trial 7'
df_segments2['trial_info'][21001:24000, ] <- 'Trial 8'
df_segments2['trial_info'][24001:27000, ] <- 'Trial 9'
df_segments2['trial_info'][27001:30000, ] <- 'Trial 10'
df_segments2['trial_info'][30001:33000, ] <- 'Trial 11'
df_segments2['trial_info'][33001:36000, ] <- 'Trial 12'
df_segments2['trial_info'][36001:39000, ] <- 'Trial 13'
df_segments2['trial_info'][39001:42000, ] <- 'Trial 14'
df_segments2['trial_info'][42001:45000, ] <- 'Trial 15'
df_segments2['trial_info'][45001:48000, ] <- 'Trial 16'
df_segments2['trial_info'][48001:51000, ] <- 'Trial 17'
df_segments2['trial_info'][51001:54000, ] <- 'Trial 18'
df_segments2['trial_info'][54001:57000, ] <- 'Trial 19'
df_segments2['trial_info'][57001:60000, ] <- 'Trial 20'

df_segments2['trial_block'][1:30000, ] <- 'Set 1'
df_segments2['trial_block'][30001:60000, ] <- 'Set 2'

df_segments3['trial_info'] <- 'A'
df_segments3['trial_block'] <- 'B'

df_segments3['trial_info'][1:3000, ] <- 'Trial 1'
df_segments3['trial_info'][3001:6000, ] <- 'Trial 2'
df_segments3['trial_info'][6001:9000, ] <- 'Trial 3'
df_segments3['trial_info'][9001:12000, ] <- 'Trial 4'
df_segments3['trial_info'][12001:15000, ] <- 'Trial 5'
df_segments3['trial_info'][15001:18000, ] <- 'Trial 6'
df_segments3['trial_info'][18001:21000, ] <- 'Trial 7'
df_segments3['trial_info'][21001:24000, ] <- 'Trial 8'
df_segments3['trial_info'][24001:27000, ] <- 'Trial 9'
df_segments3['trial_info'][27001:30000, ] <- 'Trial 10'
df_segments3['trial_info'][30001:33000, ] <- 'Trial 11'
df_segments3['trial_info'][33001:36000, ] <- 'Trial 12'
df_segments3['trial_info'][36001:39000, ] <- 'Trial 13'
df_segments3['trial_info'][39001:42000, ] <- 'Trial 14'
df_segments3['trial_info'][42001:45000, ] <- 'Trial 15'
df_segments3['trial_info'][45001:48000, ] <- 'Trial 16'
df_segments3['trial_info'][48001:51000, ] <- 'Trial 17'
df_segments3['trial_info'][51001:54000, ] <- 'Trial 18'
df_segments3['trial_info'][54001:57000, ] <- 'Trial 19'
df_segments3['trial_info'][57001:60000, ] <- 'Trial 20'

df_segments3['trial_block'][1:30000, ] <- 'Set 1'
df_segments3['trial_block'][30001:60000, ] <- 'Set 2'

df_segments4['trial_info'] <- 'A'
df_segments4['trial_block'] <- 'B'

df_segments4['trial_info'][1:3000, ] <- 'Trial 1'
df_segments4['trial_info'][3001:6000, ] <- 'Trial 2'
df_segments4['trial_info'][6001:9000, ] <- 'Trial 3'
df_segments4['trial_info'][9001:12000, ] <- 'Trial 4'
df_segments4['trial_info'][12001:15000, ] <- 'Trial 5'
df_segments4['trial_info'][15001:18000, ] <- 'Trial 6'
df_segments4['trial_info'][18001:21000, ] <- 'Trial 7'
df_segments4['trial_info'][21001:24000, ] <- 'Trial 8'
df_segments4['trial_info'][24001:27000, ] <- 'Trial 9'
df_segments4['trial_info'][27001:30000, ] <- 'Trial 10'
df_segments4['trial_info'][30001:33000, ] <- 'Trial 11'
df_segments4['trial_info'][33001:36000, ] <- 'Trial 12'
df_segments4['trial_info'][36001:39000, ] <- 'Trial 13'
df_segments4['trial_info'][39001:42000, ] <- 'Trial 14'
df_segments4['trial_info'][42001:45000, ] <- 'Trial 15'
df_segments4['trial_info'][45001:48000, ] <- 'Trial 16'
df_segments4['trial_info'][48001:51000, ] <- 'Trial 17'
df_segments4['trial_info'][51001:54000, ] <- 'Trial 18'
df_segments4['trial_info'][54001:57000, ] <- 'Trial 19'
df_segments4['trial_info'][57001:60000, ] <- 'Trial 20'

df_segments4['trial_block'][1:30000, ] <- 'Set 1'
df_segments4['trial_block'][30001:60000, ] <- 'Set 2'

FFT across each trial, across each electrode. Across each participant.

Preparing dataset

# getting trial info

trials <- unique(df_segments1['trial_info']) # info about all trials

trial_set_1 <- trials[1:10, ] # grouping variables, handy for later
trial_set_2 <- trials[11:20, ]
all_trials <- data.frame(trials=unlist(trials, use.names = FALSE)) # all trial names

Create a cumulative dataset called nest_data.

nest_data1 <- df_segments1 %>% group_by(trial_block, trial_info)
nest_data1$trial_info <- as.factor(nest_data1$trial_info)

# splitting into lists for quickly applying a function
split_df1 <- nest_data1 %>% group_split(trial_info, .keep = FALSE)
## Warning: ... is ignored in group_split(<grouped_df>), please use
## group_by(..., .add = TRUE) %>% group_split()
nest_data2 <- df_segments2 %>% group_by(trial_block, trial_info)
nest_data2$trial_info <- as.factor(nest_data2$trial_info)

# splitting into lists for quickly applying a function
split_df2 <- nest_data2 %>% group_split(trial_info, .keep = FALSE)
## Warning: ... is ignored in group_split(<grouped_df>), please use
## group_by(..., .add = TRUE) %>% group_split()
nest_data3 <- df_segments3 %>% group_by(trial_block, trial_info)
nest_data3$trial_info <- as.factor(nest_data3$trial_info)

# splitting into lists for quickly applying a function
split_df3 <- nest_data3 %>% group_split(trial_info, .keep = FALSE)
## Warning: ... is ignored in group_split(<grouped_df>), please use
## group_by(..., .add = TRUE) %>% group_split()
nest_data4 <- df_segments4 %>% group_by(trial_block, trial_info)
nest_data4$trial_info <- as.factor(nest_data4$trial_info)

# splitting into lists for quickly applying a function
split_df4 <- nest_data4 %>% group_split(trial_info, .keep = FALSE)
## Warning: ... is ignored in group_split(<grouped_df>), please use
## group_by(..., .add = TRUE) %>% group_split()

WITHIN EXPERIMENT 1 for Trial Set 1 and 2, for Participant 1

ELECTRODE WISE

split_dff1 <- split_df1[1:10] # trials 1 to 10
split_dff2 <- split_df1[11:20] # trials 1 to 20

feres <- vector('list', length(split_dff1))
  for(i in seq_along(split_dff1)){
    for(j in seq_along(split_dff1[[i]])){
      feres[[i]][[j]] <- eegfft(split_dff1[[i]][[j]], Fs=1000, lower=4, upper=30) # mod of fft
    }
  }
    
feres_2 <- vector('list', length(split_dff2))
  for(i in seq_along(split_dff2)){
    for(j in seq_along(split_dff2[[i]])){
      feres_2[[i]][[j]] <- eegfft(split_dff2[[i]][[j]], Fs=1000, lower=4, upper=30) # mod of fft
    }
  }

View(feres)
first_trial_set <- purrr::transpose(feres) # all trials electrode wise

ts1 <- vector("list", length(first_trial_set ))

for(i in seq_along(first_trial_set )){
  for(j in seq_along(first_trial_set [[i]])){
   ts1[[i]] <-  cov(first_trial_set [[i]][[j]])
}
}

second_trial_set <- purrr::transpose(feres_2) # all trials electrode wise

ts2 <- vector("list", length(second_trial_set)) 
             
for(i in seq_along(second_trial_set)){
  for(j in seq_along(second_trial_set[[i]])){
   ts2[[i]] <-  cov(second_trial_set[[i]][[j]])
}
} 
ts1ts2 <- vector("list", length(ts1))

for(i in seq_along(ts1)){
   ts1ts2[[i]] <-  data.table(ts1[i], ts2[i])
}

# perform comparisons

comparisons <- vector("list", length(ts1ts2))

for(i in seq_along(ts1ts2)){
  x <- data.frame(ts1ts2[[i]][["V1"]])
  y <- data.frame(ts1ts2[[i]][["V2"]])
   comparisons[[i]] <- testCov(x, y, method = "HD", J = 1000, alpha = 0.05, n.core = 6)
}

# get names of electrodes
names(comparisons) <- col_names

# extract electrode names, p values and statistic

cxts1ts2 <- comparisons

vars <- c("Electrode", "Statistics", "P-val")

# extract statistics and p value
location_1 <- lapply(cxts1ts2, function (x) x[c("statistics")])
location_2 <- lapply(cxts1ts2, function (x) x[c( "p.value")])

#convert statistics into dataframe column
ldf1 <- do.call(rbind, location_1)
ldf11 <- do.call(rbind,ldf1)

#convert pvalues into dataframe column
ldf2 <- do.call(rbind, location_2)

ndf_xts1ts2 <- data.frame(ldf11, ldf2)

ndf_xts1ts2$Electrode <- row.names(ndf_xts1ts2)  

new <- ndf_xts1ts2
new <- apply(new,2,as.character)

#write.csv(new,'P1E1_Electrode_Wise.csv', row.names = FALSE)

new
##       Statistic           p.value Electrode
##  [1,] "1.10591888441841"  "0.58"  "Fp1"    
##  [2,] "1.92074825439124"  "0.155" "F3"     
##  [3,] "0.480385758940315" "0.91"  "F7"     
##  [4,] "0.808702068625464" "0.763" "FT9"    
##  [5,] "1.7527672779379"   "0.213" "FC5"    
##  [6,] "1.17855237865448"  "0.484" "FC1"    
##  [7,] "1.56230954391706"  "0.3"   "C3"     
##  [8,] "1.02308963721152"  "0.61"  "T7"     
##  [9,] "1.43611943490498"  "0.348" "TP9"    
## [10,] "1.79970273977225"  "0.205" "CP5"    
## [11,] "2.27192749789194"  "0.073" "CP1"    
## [12,] "2.38233378876765"  "0.058" "Pz"     
## [13,] "2.43974870814759"  "0.04"  "P3"     
## [14,] "0.187992668264237" "0.973" "P7"     
## [15,] "2.54759885721021"  "0.023" "O1"     
## [16,] "2.49966505516486"  "0.035" "Oz"     
## [17,] "2.94248860781629"  "0.013" "O2"     
## [18,] "2.16524556792599"  "0.097" "P4"     
## [19,] "2.32199706769268"  "0.071" "P8"     
## [20,] "1.27181483168456"  "0.481" "TP10"   
## [21,] "2.23561300882731"  "0.074" "CP6"    
## [22,] "1.59392089672309"  "0.272" "CP2"    
## [23,] "0.642469814310713" "0.805" "Cz"     
## [24,] "1.88773282246755"  "0.156" "C4"     
## [25,] "0.942668371765889" "0.654" "T8"     
## [26,] "1.30586203833847"  "0.479" "FT10"   
## [27,] "0.734940247843266" "0.778" "FC6"    
## [28,] "1.1310201089237"   "0.524" "FC2"    
## [29,] "1.56079902228653"  "0.28"  "F4"     
## [30,] "1.21881505060521"  "0.527" "F8"     
## [31,] "0.809457844814886" "0.725" "Fp2"
ts12 <- ggplot(ndf_xts1ts2, aes(reorder(Electrode, Statistic), Statistic)) +
  geom_col(aes(fill=p.value<0.05)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Electrodes significant in Trial 1 
       versus Trial 2 sorted by HD statistic
       for Participant 1 Experiment 1") + 
  theme_minimal()

#png('P1E1_Electrode_Wise_point_05.#png', units="in", width=5, height=5, res=300)
ts12

#dev.off()

ts12

ts12 <- ggplot(ndf_xts1ts2, aes(reorder(Electrode, Statistic), Statistic)) +
  geom_col(aes(fill=p.value<0.10)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Electrodes significant in Trial 1 
       versus Trial 2 sorted by HD statistic
       for Participant 1 Experiment 1") + 
  theme_minimal()

#png('P1E1_Electrode_Wise_point_01.#png', units="in", width=5, height=5, res=300)
ts12

#dev.off()

ts12

TRIAL SET WISE

first_trial_block <- feres # all trials block wise

tb1 <- vector("list", length(first_trial_block ))

for(i in seq_along(first_trial_block )){
  for(j in seq_along(first_trial_block [[i]])){
   tb1[[i]] <-  cov(first_trial_block [[i]][[j]])
}
}

second_trial_block <- feres_2 # all trials block wise

tb2 <- vector("list", length(second_trial_block)) 
             
for(i in seq_along(second_trial_block)){
  for(j in seq_along(second_trial_block[[i]])){
   tb2[[i]] <-  cov(second_trial_block[[i]][[j]])
}
}  
tb1tb2 <- vector("list", length(tb1))

for(i in seq_along(tb1)){
   tb1tb2[[i]] <-  data.table(tb1[i], tb2[i])
}

# perform comparisons

comparisons <- vector("list", length(tb1tb2))

for(i in seq_along(tb1tb2)){
  x <- data.frame(tb1tb2[[i]][["V1"]])
  y <- data.frame(tb1tb2[[i]][["V2"]])
   comparisons[[i]] <- testCov(x, y, method = "HD", J = 1000, alpha = 0.05, n.core = 6)
}

# get names of electrodes
names(comparisons) <- trials$trial_info[1:10]


# extract electrode names, p values and statistic

cxtb1tb2 <- comparisons

vars <- c("Electrode", "Statistics", "P-val")

# extract statistics and p value
location_1 <- lapply(cxtb1tb2, function (x) x[c("statistics")])
location_2 <- lapply(cxtb1tb2, function (x) x[c( "p.value")])

#convert statistics into dataframe column
ldf1 <- do.call(rbind, location_1)
ldf11 <- do.call(rbind,ldf1)

#convert pvalues into dataframe column
ldf2 <- do.call(rbind, location_2)

ndf_xtb1tb2 <- data.frame(ldf11, ldf2)

ndf_xtb1tb2$Trials <- row.names(ndf_xtb1tb2)


new_t <- ndf_xtb1tb2  
new_t <- apply(new_t,2,as.character)

#write.csv(new_t,'P1E1_Trial_set_Wise.csv', row.names = FALSE)

new_t
##       Statistic           p.value Trials    
##  [1,] "2.38602284733244"  "0.056" "Trial 1" 
##  [2,] "1.25536779743045"  "0.503" "Trial 2" 
##  [3,] "1.96295771938135"  "0.149" "Trial 3" 
##  [4,] "0.609087081168475" "0.822" "Trial 4" 
##  [5,] "1.84977849630914"  "0.169" "Trial 5" 
##  [6,] "1.14802803807073"  "0.534" "Trial 6" 
##  [7,] "1.14294368341554"  "0.543" "Trial 7" 
##  [8,] "2.09316335857992"  "0.108" "Trial 8" 
##  [9,] "1.47341709406703"  "0.33"  "Trial 9" 
## [10,] "0.809457844814886" "0.717" "Trial 10"
tb12 <- ggplot(ndf_xtb1tb2, aes(reorder(Trials, Statistic), Statistic))  +
  geom_col(aes(fill=p.value<0.05)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Trials significant in Trial block 1 
       versus Trial block 2 sorted by HD statistic
       for Participant 1 Experiment 1") + 
  theme_minimal()

#png('P1E1_Trial_block_Wise_point_05.#png', units="in", width=5, height=5, res=300)
tb12

#dev.off()

tb12

tb12 <- ggplot(ndf_xtb1tb2, aes(reorder(Trials, Statistic), Statistic))  +
  geom_col(aes(fill=p.value<0.10)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Trials significant in Trial block 1 
       versus Trial block 2 sorted by HD statistic
       for Participant 1 Experiment 1") + 
  theme_minimal()

#png('P1E1_Trial_block_Wise_point_01.#png', units="in", width=5, height=5, res=300)
tb12

#dev.off()

tb12

WITHIN EXPERIMENT 2 for Trial Set 1 and 2, for Participant 1 Experiment 2

ELECTRODE WISE

split_dff1 <- split_df2[1:10] # trials 1 to 10
split_dff2 <- split_df2[11:20] # trials 1 to 20

feres <- vector('list', length(split_dff1))
  for(i in seq_along(split_dff1)){
    for(j in seq_along(split_dff1[[i]])){
      feres[[i]][[j]] <- eegfft(split_dff1[[i]][[j]], Fs=1000, lower=4, upper=40) # mod of fft
    }
  }

feres_2 <- vector('list', length(split_dff2))
  for(i in seq_along(split_dff2)){
    for(j in seq_along(split_dff2[[i]])){
      feres_2[[i]][[j]] <- eegfft(split_dff2[[i]][[j]], Fs=1000, lower=4, upper=40) # mod of fft
    }
  }
first_trial_set <- purrr::transpose(feres) # all trials electrode wise

ts1 <- vector("list", length(first_trial_set ))

for(i in seq_along(first_trial_set )){
  for(j in seq_along(first_trial_set [[i]])){
   ts1[[i]] <-  cov(first_trial_set [[i]][[j]])
}
}

second_trial_set <- purrr::transpose(feres_2) # all trials electrode wise

ts2 <- vector("list", length(second_trial_set)) 
             
for(i in seq_along(second_trial_set)){
  for(j in seq_along(second_trial_set[[i]])){
   ts2[[i]] <-  cov(second_trial_set[[i]][[j]])
}
} 
ts1ts2 <- vector("list", length(ts1))

for(i in seq_along(ts1)){
   ts1ts2[[i]] <-  data.table(ts1[i], ts2[i])
}

# perform comparisons

comparisons <- vector("list", length(ts1ts2))

for(i in seq_along(ts1ts2)){
  x <- data.frame(ts1ts2[[i]][["V1"]])
  y <- data.frame(ts1ts2[[i]][["V2"]])
   comparisons[[i]] <- testCov(x, y, method = "HD", J = 1000, alpha = 0.05, n.core = 6)
}

# get names of electrodes
names(comparisons) <- col_names

# extract electrode names, p values and statistic

cxts1ts2 <- comparisons

vars <- c("Electrode", "Statistics", "P-val")

# extract statistics and p value
location_1 <- lapply(cxts1ts2, function (x) x[c("statistics")])
location_2 <- lapply(cxts1ts2, function (x) x[c( "p.value")])

#convert statistics into dataframe column
ldf1 <- do.call(rbind, location_1)
ldf11 <- do.call(rbind,ldf1)

#convert pvalues into dataframe column
ldf2 <- do.call(rbind, location_2)

ndf_xts1ts2 <- data.frame(ldf11, ldf2)

ndf_xts1ts2$Electrode <- row.names(ndf_xts1ts2)  

new <- ndf_xts1ts2
new <- apply(new,2,as.character)

#write.csv(new,'P1E2_Electrode_Wise.csv', row.names = FALSE)

new
##       Statistic           p.value Electrode
##  [1,] "1.22303039975706"  "0.498" "Fp1"    
##  [2,] "1.78778346814675"  "0.175" "F3"     
##  [3,] "1.62440107644807"  "0.272" "F7"     
##  [4,] "0.494786545485926" "0.881" "FT9"    
##  [5,] "0.944464092473533" "0.631" "FC5"    
##  [6,] "0.658295334126862" "0.793" "FC1"    
##  [7,] "0.634454905296141" "0.816" "C3"     
##  [8,] "2.18418118819138"  "0.094" "T7"     
##  [9,] "0.31970382351246"  "0.956" "TP9"    
## [10,] "0.627849763691918" "0.843" "CP5"    
## [11,] "0.712556423573813" "0.78"  "CP1"    
## [12,] "1.68889322269718"  "0.218" "Pz"     
## [13,] "1.41034056613503"  "0.389" "P3"     
## [14,] "0.546864084727157" "0.874" "P7"     
## [15,] "1.57300033906367"  "0.315" "O1"     
## [16,] "2.11058422532536"  "0.113" "Oz"     
## [17,] "1.8127456518621"   "0.183" "O2"     
## [18,] "0.465880240338121" "0.916" "P4"     
## [19,] "0.774958817958415" "0.745" "P8"     
## [20,] "1.35881025339073"  "0.401" "TP10"   
## [21,] "1.06154355832133"  "0.543" "CP6"    
## [22,] "0.541399815118614" "0.892" "CP2"    
## [23,] "2.32380892227943"  "0.056" "Cz"     
## [24,] "0.908561339143798" "0.705" "C4"     
## [25,] "1.41149728701037"  "0.355" "T8"     
## [26,] "0.925075091592244" "0.704" "FT10"   
## [27,] "0.95339410772391"  "0.644" "FC6"    
## [28,] "1.60836596621295"  "0.299" "FC2"    
## [29,] "1.0200180821319"   "0.631" "F4"     
## [30,] "1.10290598960864"  "0.601" "F8"     
## [31,] "1.17457350674517"  "0.492" "Fp2"
ts12 <- ggplot(ndf_xts1ts2, aes(reorder(Electrode, Statistic), Statistic)) +
  geom_col(aes(fill=p.value<0.05)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Electrodes significant in Trial 1 
       versus Trial 2 sorted by HD statistic
       for Participant 1 Experiment 2") + 
  theme_minimal()

#png('P1E2_Electrode_Wise_point_05.#png', units="in", width=5, height=5, res=300)
ts12

#dev.off()

ts12

ts12 <- ggplot(ndf_xts1ts2, aes(reorder(Electrode, Statistic), Statistic)) +
  geom_col(aes(fill=p.value<0.10)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Electrodes significant in Trial 1 
       versus Trial 2 sorted by HD statistic
       for Participant 1 Experiment 2") + 
  theme_minimal()

#png('P1E2_Electrode_Wise_point_01.#png', units="in", width=5, height=5, res=300)
ts12

#dev.off()

ts12

TRIAL SET WISE

first_trial_block <- feres # all trials block wise

tb1 <- vector("list", length(first_trial_block ))

for(i in seq_along(first_trial_block )){
  for(j in seq_along(first_trial_block [[i]])){
   tb1[[i]] <-  cov(first_trial_block [[i]][[j]])
}
}

second_trial_block <- feres_2 # all trials block wise

tb2 <- vector("list", length(second_trial_block)) 
             
for(i in seq_along(second_trial_block)){
  for(j in seq_along(second_trial_block[[i]])){
   tb2[[i]] <-  cov(second_trial_block[[i]][[j]])
}
}  
tb1tb2 <- vector("list", length(tb1))

for(i in seq_along(tb1)){
   tb1tb2[[i]] <-  data.table(tb1[i], tb2[i])
}

# perform comparisons

comparisons <- vector("list", length(tb1tb2))

for(i in seq_along(tb1tb2)){
  x <- data.frame(tb1tb2[[i]][["V1"]])
  y <- data.frame(tb1tb2[[i]][["V2"]])
   comparisons[[i]] <- testCov(x, y, method = "HD", J = 1000, alpha = 0.05, n.core = 6)
}

# get names of electrodes
names(comparisons) <- trials$trial_info[1:10]


# extract electrode names, p values and statistic

cxtb1tb2 <- comparisons

vars <- c("Electrode", "Statistics", "P-val")

# extract statistics and p value
location_1 <- lapply(cxtb1tb2, function (x) x[c("statistics")])
location_2 <- lapply(cxtb1tb2, function (x) x[c( "p.value")])

#convert statistics into dataframe column
ldf1 <- do.call(rbind, location_1)
ldf11 <- do.call(rbind,ldf1)

#convert pvalues into dataframe column
ldf2 <- do.call(rbind, location_2)

ndf_xtb1tb2 <- data.frame(ldf11, ldf2)

ndf_xtb1tb2$Trials <- row.names(ndf_xtb1tb2)

new_x <- ndf_xtb1tb2
new_x <- apply(new_x,2,as.character)

#write.csv(new_x,'P1E2_Trial_block_Wise.csv', row.names = FALSE)

new_x
##       Statistic           p.value Trials    
##  [1,] "1.68815599043392"  "0.246" "Trial 1" 
##  [2,] "1.76522017903762"  "0.207" "Trial 2" 
##  [3,] "1.89287489898129"  "0.16"  "Trial 3" 
##  [4,] "2.02380466921513"  "0.112" "Trial 4" 
##  [5,] "0.380528796493255" "0.927" "Trial 5" 
##  [6,] "0.908680070214235" "0.64"  "Trial 6" 
##  [7,] "0.462702543688756" "0.91"  "Trial 7" 
##  [8,] "1.73747387107646"  "0.197" "Trial 8" 
##  [9,] "0.598978753031354" "0.817" "Trial 9" 
## [10,] "1.17457350674517"  "0.479" "Trial 10"
tb12 <- ggplot(ndf_xtb1tb2, aes(reorder(Trials, Statistic), Statistic))  +
  geom_col(aes(fill=p.value<0.05)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Trials significant in Trial block 1 
       versus Trial block 2 sorted by HD statistic
       for Participant 1 Experiment 2") + 
  theme_minimal()
#png('P1E2_Trial_block_Wise_point_05.#png', units="in", width=5, height=5, res=300)
tb12

#dev.off()

tb12

tb12 <- ggplot(ndf_xtb1tb2, aes(reorder(Trials, Statistic), Statistic))  +
  geom_col(aes(fill=p.value<0.10)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Trials significant in Trial block 1 
       versus Trial block 2 sorted by HD statistic
       for Participant 1 Experiment 2") + 
  theme_minimal()

#png('P1E2_Trial_block_Wise_point_01.#png', units="in", width=5, height=5, res=300)
tb12

#dev.off()

tb12

WITHIN EXPERIMENT 1 for Trial Set 1 and 2, for Participant 2 Experiment 1 ELECTRODE WISE

split_dff1 <- split_df3[1:10] # trials 1 to 10
split_dff2 <- split_df3[11:20] # trials 1 to 20

feres <- vector('list', length(split_dff1))
  for(i in seq_along(split_dff1)){
    for(j in seq_along(split_dff1[[i]])){
      feres[[i]][[j]] <- eegfft(split_dff1[[i]][[j]], Fs=1000, lower=4, upper=40) # mod of fft
    }
  }

feres_2 <- vector('list', length(split_dff2))
  for(i in seq_along(split_dff2)){
    for(j in seq_along(split_dff2[[i]])){
      feres_2[[i]][[j]] <- eegfft(split_dff2[[i]][[j]], Fs=1000, lower=4, upper=40) # mod of fft
    }
  }
first_trial_set <- purrr::transpose(feres) # all trials electrode wise

ts1 <- vector("list", length(first_trial_set ))

for(i in seq_along(first_trial_set )){
  for(j in seq_along(first_trial_set [[i]])){
   ts1[[i]] <-  cov(first_trial_set [[i]][[j]])
}
}

second_trial_set <- purrr::transpose(feres_2) # all trials electrode wise

ts2 <- vector("list", length(second_trial_set)) 
             
for(i in seq_along(second_trial_set)){
  for(j in seq_along(second_trial_set[[i]])){
   ts2[[i]] <-  cov(second_trial_set[[i]][[j]])
}
} 
ts1ts2 <- vector("list", length(ts1))

for(i in seq_along(ts1)){
   ts1ts2[[i]] <-  data.table(ts1[i], ts2[i])
}

# perform comparisons

comparisons <- vector("list", length(ts1ts2))

for(i in seq_along(ts1ts2)){
  x <- data.frame(ts1ts2[[i]][["V1"]])
  y <- data.frame(ts1ts2[[i]][["V2"]])
   comparisons[[i]] <- testCov(x, y, method = "HD", J = 1000, alpha = 0.05, n.core = 6)
}

# get names of electrodes
names(comparisons) <- col_names

# extract electrode names, p values and statistic

cxts1ts2 <- comparisons

vars <- c("Electrode", "Statistics", "P-val")

# extract statistics and p value
location_1 <- lapply(cxts1ts2, function (x) x[c("statistics")])
location_2 <- lapply(cxts1ts2, function (x) x[c( "p.value")])

#convert statistics into dataframe column
ldf1 <- do.call(rbind, location_1)
ldf11 <- do.call(rbind,ldf1)

#convert pvalues into dataframe column
ldf2 <- do.call(rbind, location_2)

ndf_xts1ts2 <- data.frame(ldf11, ldf2)

ndf_xts1ts2$Electrode <- row.names(ndf_xts1ts2)  

new <- ndf_xts1ts2
new <- apply(new,2,as.character)  

#write.csv(new,'P2E1_Electrode_Wise.csv', row.names = FALSE)

new
##       Statistic           p.value Electrode
##  [1,] "1.76092508602303"  "0.21"  "Fp1"    
##  [2,] "1.62178043173592"  "0.235" "F3"     
##  [3,] "1.53782382151661"  "0.319" "F7"     
##  [4,] "2.15951825608037"  "0.098" "FT9"    
##  [5,] "1.55755333715803"  "0.331" "FC5"    
##  [6,] "1.90348673664505"  "0.153" "FC1"    
##  [7,] "1.82980407696519"  "0.175" "C3"     
##  [8,] "1.29024147225616"  "0.435" "T7"     
##  [9,] "1.39937671692119"  "0.39"  "TP9"    
## [10,] "1.63285775012515"  "0.282" "CP5"    
## [11,] "1.83372627096089"  "0.191" "CP1"    
## [12,] "1.82263405459415"  "0.235" "Pz"     
## [13,] "2.00834715488513"  "0.14"  "P3"     
## [14,] "1.96427633591943"  "0.135" "P7"     
## [15,] "1.99201804102181"  "0.149" "O1"     
## [16,] "2.15269753558635"  "0.105" "Oz"     
## [17,] "2.10473205495031"  "0.095" "O2"     
## [18,] "2.1884298315297"   "0.089" "P4"     
## [19,] "2.12548729605959"  "0.129" "P8"     
## [20,] "2.15351844600292"  "0.088" "TP10"   
## [21,] "1.48101514958641"  "0.358" "CP6"    
## [22,] "2.81167777033037"  "0.018" "CP2"    
## [23,] "2.20688766014457"  "0.104" "Cz"     
## [24,] "2.24524044281677"  "0.074" "C4"     
## [25,] "1.32913789019492"  "0.33"  "T8"     
## [26,] "0.467370212535788" "0.926" "FT10"   
## [27,] "1.30606742532448"  "0.397" "FC6"    
## [28,] "1.79376275759592"  "0.15"  "FC2"    
## [29,] "1.61601307205432"  "0.234" "F4"     
## [30,] "0.316089509929186" "0.955" "F8"     
## [31,] "0.874524332518535" "0.681" "Fp2"
ts12 <- ggplot(ndf_xts1ts2, aes(reorder(Electrode, Statistic), Statistic)) +
  geom_col(aes(fill=p.value<0.05)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Electrodes significant in Trial 1 
       versus Trial 2 sorted by HD statistic
       for Participant 2 Experiment 1") + 
  theme_minimal()

#png('P2E1_Electrode_Wise_point_05.#png', units="in", width=5, height=5, res=300)
ts12

#dev.off()

ts12

ts12 <- ggplot(ndf_xts1ts2, aes(reorder(Electrode, Statistic), Statistic)) +
  geom_col(aes(fill=p.value<0.10)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Electrodes significant in Trial 1 
       versus Trial 2 sorted by HD statistic
       for Participant 2 Experiment 1") + 
  theme_minimal()

#png('P2E1_Electrode_Wise_point_01.#png', units="in", width=5, height=5, res=300)
ts12

#dev.off()

ts12

TRIAL SET WISE

first_trial_block <- feres # all trials block wise

tb1 <- vector("list", length(first_trial_block ))

for(i in seq_along(first_trial_block )){
  for(j in seq_along(first_trial_block [[i]])){
   tb1[[i]] <-  cov(first_trial_block [[i]][[j]])
}
}

second_trial_block <- feres_2 # all trials block wise

tb2 <- vector("list", length(second_trial_block)) 
             
for(i in seq_along(second_trial_block)){
  for(j in seq_along(second_trial_block[[i]])){
   tb2[[i]] <-  cov(second_trial_block[[i]][[j]])
}
}  
tb1tb2 <- vector("list", length(tb1))

for(i in seq_along(tb1)){
   tb1tb2[[i]] <-  data.table(tb1[i], tb2[i])
}

# perform comparisons

comparisons <- vector("list", length(tb1tb2))

for(i in seq_along(tb1tb2)){
  x <- data.frame(tb1tb2[[i]][["V1"]])
  y <- data.frame(tb1tb2[[i]][["V2"]])
   comparisons[[i]] <- testCov(x, y, method = "HD", J = 1000, alpha = 0.05, n.core = 6)
}

# get names of electrodes
names(comparisons) <- trials$trial_info[1:10]


# extract electrode names, p values and statistic

cxtb1tb2 <- comparisons

vars <- c("Electrode", "Statistics", "P-val")

# extract statistics and p value
location_1 <- lapply(cxtb1tb2, function (x) x[c("statistics")])
location_2 <- lapply(cxtb1tb2, function (x) x[c( "p.value")])

#convert statistics into dataframe column
ldf1 <- do.call(rbind, location_1)
ldf11 <- do.call(rbind,ldf1)

#convert pvalues into dataframe column
ldf2 <- do.call(rbind, location_2)

ndf_xtb1tb2 <- data.frame(ldf11, ldf2)

ndf_xtb1tb2$Trials <- row.names(ndf_xtb1tb2)

new <- ndf_xtb1tb2
new <- apply(new,2,as.character)

#write.csv(new,'P2E1_Trial_block_Wise.csv', row.names = FALSE)

new
##       Statistic           p.value Trials    
##  [1,] "1.93523654857661"  "0.164" "Trial 1" 
##  [2,] "1.03510242186339"  "0.58"  "Trial 2" 
##  [3,] "2.33196296259108"  "0.067" "Trial 3" 
##  [4,] "0.656606498161964" "0.812" "Trial 4" 
##  [5,] "0.955035524362911" "0.659" "Trial 5" 
##  [6,] "1.74405004320338"  "0.275" "Trial 6" 
##  [7,] "1.94688896099381"  "0.143" "Trial 7" 
##  [8,] "1.82647997819967"  "0.168" "Trial 8" 
##  [9,] "0.317912446509795" "0.937" "Trial 9" 
## [10,] "0.874524332518535" "0.666" "Trial 10"
tb12 <- ggplot(ndf_xtb1tb2, aes(reorder(Trials, Statistic), Statistic))  +
  geom_col(aes(fill=p.value<0.05)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Trials significant in Trial block 1 
       versus Trial block 2 sorted by HD statistic
       for Participant 2 Experiment 1") + 
  theme_minimal()
#png('P2E1_Trial_block_Wise_point_05.#png', units="in", width=5, height=5, res=300)
tb12

#dev.off()

tb12

tb12 <- ggplot(ndf_xtb1tb2, aes(reorder(Trials, Statistic), Statistic))  +
  geom_col(aes(fill=p.value<0.10)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Trials significant in Trial block 1 
       versus Trial block 2 sorted by HD statistic
       for Participant 2 Experiment 1") + 
  theme_minimal()

#png('P2E1_Trial_block_Wise_point_01.#png', units="in", width=5, height=5, res=300)
tb12

#dev.off()

tb12

WITHIN EXPERIMENT 1 for Trial Set 1 and 2, for Participant 2 Experiment 2 ELECTRODE WISE

split_dff1 <- split_df4[1:10] # trials 1 to 10
split_dff2 <- split_df4[11:20] # trials 1 to 20

feres <- vector('list', length(split_dff1))
  for(i in seq_along(split_dff1)){
    for(j in seq_along(split_dff1[[i]])){
      feres[[i]][[j]] <- eegfft(split_dff1[[i]][[j]], Fs=1000, lower=4, upper=40) # mod of fft
    }
  }

feres_2 <- vector('list', length(split_dff2))
  for(i in seq_along(split_dff2)){
    for(j in seq_along(split_dff2[[i]])){
      feres_2[[i]][[j]] <- eegfft(split_dff2[[i]][[j]], Fs=1000, lower=4, upper=40) # mod of fft
    }
  }
first_trial_set <- purrr::transpose(feres) # all trials electrode wise

ts1 <- vector("list", length(first_trial_set ))

for(i in seq_along(first_trial_set )){
  for(j in seq_along(first_trial_set [[i]])){
   ts1[[i]] <-  cov(first_trial_set [[i]][[j]])
}
}

second_trial_set <- purrr::transpose(feres_2) # all trials electrode wise

ts2 <- vector("list", length(second_trial_set)) 
             
for(i in seq_along(second_trial_set)){
  for(j in seq_along(second_trial_set[[i]])){
   ts2[[i]] <-  cov(second_trial_set[[i]][[j]])
}
} 
ts1ts2 <- vector("list", length(ts1))

for(i in seq_along(ts1)){
   ts1ts2[[i]] <-  data.table(ts1[i], ts2[i])
}

# perform comparisons

comparisons <- vector("list", length(ts1ts2))

for(i in seq_along(ts1ts2)){
  x <- data.frame(ts1ts2[[i]][["V1"]])
  y <- data.frame(ts1ts2[[i]][["V2"]])
   comparisons[[i]] <- testCov(x, y, method = "HD", J = 1000, alpha = 0.05, n.core = 6)
}

# get names of electrodes
names(comparisons) <- col_names

# extract electrode names, p values and statistic

cxts1ts2 <- comparisons

vars <- c("Electrode", "Statistics", "P-val")

# extract statistics and p value
location_1 <- lapply(cxts1ts2, function (x) x[c("statistics")])
location_2 <- lapply(cxts1ts2, function (x) x[c( "p.value")])

#convert statistics into dataframe column
ldf1 <- do.call(rbind, location_1)
ldf11 <- do.call(rbind,ldf1)

#convert pvalues into dataframe column
ldf2 <- do.call(rbind, location_2)

ndf_xts1ts2 <- data.frame(ldf11, ldf2)
ndf_xts1ts2$Electrode <- row.names(ndf_xts1ts2)  

new <- ndf_xts1ts2 
new <- apply(new,2,as.character) 

#write.csv(new,'P2E2_Electrode_Wise.csv', row.names = FALSE)

new
##       Statistic           p.value Electrode
##  [1,] "0.958568577598474" "0.644" "Fp1"    
##  [2,] "1.6907175033549"   "0.267" "F3"     
##  [3,] "1.10955801027993"  "0.529" "F7"     
##  [4,] "0.98440140203093"  "0.621" "FT9"    
##  [5,] "1.12489682870616"  "0.582" "FC5"    
##  [6,] "1.01750966500199"  "0.628" "FC1"    
##  [7,] "1.6064960234909"   "0.258" "C3"     
##  [8,] "1.14237950898756"  "0.524" "T7"     
##  [9,] "0.900236024977678" "0.679" "TP9"    
## [10,] "1.18034896608389"  "0.504" "CP5"    
## [11,] "2.90810023418223"  "0.015" "CP1"    
## [12,] "2.22614171656697"  "0.065" "Pz"     
## [13,] "2.69831804124542"  "0.025" "P3"     
## [14,] "2.5540967100396"   "0.039" "P7"     
## [15,] "2.29284901270995"  "0.057" "O1"     
## [16,] "2.80136960700315"  "0.016" "Oz"     
## [17,] "2.02119718645316"  "0.13"  "O2"     
## [18,] "2.06866070028585"  "0.11"  "P4"     
## [19,] "2.63468191457758"  "0.029" "P8"     
## [20,] "2.58529829060601"  "0.029" "TP10"   
## [21,] "2.81533946778307"  "0.017" "CP6"    
## [22,] "2.97910018571557"  "0.007" "CP2"    
## [23,] "2.82311202883718"  "0.013" "Cz"     
## [24,] "2.22403954024284"  "0.062" "C4"     
## [25,] "2.45647545622448"  "0.05"  "T8"     
## [26,] "2.67697884483983"  "0.021" "FT10"   
## [27,] "2.43733468235963"  "0.053" "FC6"    
## [28,] "1.34140820026713"  "0.459" "FC2"    
## [29,] "2.52444267344597"  "0.034" "F4"     
## [30,] "2.91940808376706"  "0.012" "F8"     
## [31,] "1.90716917419187"  "0.161" "Fp2"
ts12 <- ggplot(ndf_xts1ts2, aes(reorder(Electrode, Statistic), Statistic)) +
  geom_col(aes(fill=p.value<0.05)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Electrodes significant in Trial 1 
       versus Trial 2 sorted by HD statistic
       for Participant 2 Experiment 2") + 
  theme_minimal()

#png('P2E2_Electrode_Wise_point_05.#png', units="in", width=5, height=5, res=300)
ts12

#dev.off()

ts12

ts12 <- ggplot(ndf_xts1ts2, aes(reorder(Electrode, Statistic), Statistic)) +
  geom_col(aes(fill=p.value<0.10)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Electrodes significant in Trial 1 
       versus Trial 2 sorted by HD statistic
       for Participant 2 Experiment 2") + 
  theme_minimal()

#png('P2E2_Electrode_Wise_point_01.#png', units="in", width=5, height=5, res=300)
ts12

#dev.off()

ts12

TRIAL SET WISE

first_trial_block <- feres # all trials block wise

tb1 <- vector("list", length(first_trial_block ))

for(i in seq_along(first_trial_block )){
  for(j in seq_along(first_trial_block [[i]])){
   tb1[[i]] <-  cov(first_trial_block [[i]][[j]])
}
}

second_trial_block <- feres_2 # all trials block wise

tb2 <- vector("list", length(second_trial_block)) 
             
for(i in seq_along(second_trial_block)){
  for(j in seq_along(second_trial_block[[i]])){
   tb2[[i]] <-  cov(second_trial_block[[i]][[j]])
}
}  
tb1tb2 <- vector("list", length(tb1))

for(i in seq_along(tb1)){
   tb1tb2[[i]] <-  data.table(tb1[i], tb2[i])
}

# perform comparisons

comparisons <- vector("list", length(tb1tb2))

for(i in seq_along(tb1tb2)){
  x <- data.frame(tb1tb2[[i]][["V1"]])
  y <- data.frame(tb1tb2[[i]][["V2"]])
   comparisons[[i]] <- testCov(x, y, method = "HD", J = 1000, alpha = 0.05, n.core = 6)
}

# get names of electrodes
names(comparisons) <- trials$trial_info[1:10]


# extract electrode names, p values and statistic

cxtb1tb2 <- comparisons

vars <- c("Electrode", "Statistics", "P-val")

# extract statistics and p value
location_1 <- lapply(cxtb1tb2, function (x) x[c("statistics")])
location_2 <- lapply(cxtb1tb2, function (x) x[c( "p.value")])

#convert statistics into dataframe column
ldf1 <- do.call(rbind, location_1)
ldf11 <- do.call(rbind,ldf1)

#convert pvalues into dataframe column
ldf2 <- do.call(rbind, location_2)

ndf_xtb1tb2 <- data.frame(ldf11, ldf2)

ndf_xtb1tb2$Trials <- row.names(ndf_xtb1tb2)

new <- ndf_xtb1tb2
new <- apply(new,2,as.character)

#write.csv(new,'P2E2_Trial_block_Wise.csv')

new
##       Statistic           p.value Trials    
##  [1,] "0.876323831811388" "0.646" "Trial 1" 
##  [2,] "1.24139542789022"  "0.451" "Trial 2" 
##  [3,] "1.93099715134095"  "0.141" "Trial 3" 
##  [4,] "1.19394382378729"  "0.533" "Trial 4" 
##  [5,] "1.29339547724303"  "0.469" "Trial 5" 
##  [6,] "1.66797887779341"  "0.204" "Trial 6" 
##  [7,] "1.57892194052562"  "0.243" "Trial 7" 
##  [8,] "1.40432456311273"  "0.398" "Trial 8" 
##  [9,] "2.79042978047496"  "0.011" "Trial 9" 
## [10,] "1.90716917419187"  "0.167" "Trial 10"
tb12 <- ggplot(ndf_xtb1tb2, aes(reorder(Trials, Statistic), Statistic))  +
  geom_col(aes(fill=p.value<0.05)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Trials significant in Trial block 1 
       versus Trial block 2 sorted by HD statistic
       for Participant 2 Experiment 2") + 
  theme_minimal()
#png('P2E2_Trial_block_Wise_point_05.#png', units="in", width=5, height=5, res=300)
tb12

#dev.off()

tb12

tb12 <- ggplot(ndf_xtb1tb2, aes(reorder(Trials, Statistic), Statistic))  +
  geom_col(aes(fill=p.value<0.10)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Trials significant in Trial block 1 
       versus Trial block 2 sorted by HD statistic
       for Participant 2 Experiment 2") + 
  theme_minimal()

#png('P2E2_Trial_block_Wise_point_01.#png', units="in", width=5, height=5, res=300)
tb12

#dev.off()

tb12

BETWEEN EXPERIMENTS 1 and 2 for Participant 1

eres <- vector('list', length(split_df1))
  for(i in seq_along(split_df1)){
    for(j in seq_along(split_df1[[i]])){
      eres[[i]][[j]] <- eegfft(split_df1[[i]][[j]], Fs=1000, lower=4, upper=40) # mod of fft
    }
  }

eres_2 <- vector('list', length(split_df2))
  for(i in seq_along(split_df2)){
    for(j in seq_along(split_df2[[i]])){
      eres_2[[i]][[j]] <- eegfft(split_df2[[i]][[j]], Fs=1000, lower=4, upper=40) # mod of fft
    }
  }

ELECTRODE WISE

first_exp <- purrr::transpose(eres) # all trials electrode wise

l1 <- vector("list", length(first_exp ))

for(i in seq_along(first_exp )){
  for(j in seq_along(first_exp [[i]])){
   l1[[i]] <-  cov(first_exp [[i]][[j]])
}
}

second_exp <- purrr::transpose(eres_2) # all trials electrode wise

l2 <- vector("list", length(second_exp)) 
             
for(i in seq_along(second_exp)){
  for(j in seq_along(second_exp[[i]])){
   l2[[i]] <-  cov(second_exp[[i]][[j]])
}
} 
l1l2 <- vector("list", length(l1))

for(i in seq_along(l1)){
   l1l2[[i]] <-  data.table(l1[i], l2[i])
}

# perform comparisons

comparisons <- vector("list", length(l1l2))

for(i in seq_along(l1l2)){
  x <- data.frame(l1l2[[i]][["V1"]])
  y <- data.frame(l1l2[[i]][["V2"]])
   comparisons[[i]] <- testCov(x, y, method = "HD", J = 1000, alpha = 0.05, n.core = 6)
}

# get names of electrodes
names(comparisons) <- col_names

# extract electrode names, p values and statistic

cx <- comparisons

vars <- c("Electrode", "Statistics", "P-val")

# extract statistics and p value
location_1 <- lapply(cx, function (x) x[c("statistics")])
location_2 <- lapply(cx, function (x) x[c( "p.value")])

#convert statistics into dataframe column
ldf1 <- do.call(rbind, location_1)
ldf11 <- do.call(rbind,ldf1)

#convert pvalues into dataframe column
ldf2 <- do.call(rbind, location_2)

ndf_x <- data.frame(ldf11, ldf2)

ndf_x$Electrode <- row.names(ndf_x)

new <- ndf_x
new <- apply(new,2,as.character)

#write.csv(new,'P1E1_P1E2_Electrode_Wise.csv', row.names = FALSE)

new
##       Statistic           p.value Electrode
##  [1,] "0.704223800720052" "0.789" "Fp1"    
##  [2,] "0.400486906172195" "0.918" "F3"     
##  [3,] "0.511678135020762" "0.899" "F7"     
##  [4,] "1.36651746907256"  "0.396" "FT9"    
##  [5,] "1.84132151270553"  "0.177" "FC5"    
##  [6,] "1.21956690787388"  "0.501" "FC1"    
##  [7,] "1.60732051706135"  "0.273" "C3"     
##  [8,] "2.39132053659314"  "0.06"  "T7"     
##  [9,] "0.344531486665895" "0.947" "TP9"    
## [10,] "1.59663996828706"  "0.291" "CP5"    
## [11,] "1.70492610755417"  "0.245" "CP1"    
## [12,] "1.73243998783523"  "0.224" "Pz"     
## [13,] "1.37036895219263"  "0.39"  "P3"     
## [14,] "1.7262269145429"   "0.203" "P7"     
## [15,] "1.36452791866192"  "0.35"  "O1"     
## [16,] "0.816630592113084" "0.731" "Oz"     
## [17,] "1.44992852921217"  "0.362" "O2"     
## [18,] "1.95804975687914"  "0.165" "P4"     
## [19,] "0.974757632469408" "0.655" "P8"     
## [20,] "0.627151875197742" "0.81"  "TP10"   
## [21,] "2.32735554622666"  "0.072" "CP6"    
## [22,] "0.917289484743218" "0.656" "CP2"    
## [23,] "2.29090227918677"  "0.06"  "Cz"     
## [24,] "1.65602803318305"  "0.274" "C4"     
## [25,] "0.893462077357707" "0.697" "T8"     
## [26,] "1.29669705810268"  "0.412" "FT10"   
## [27,] "0.429967954542962" "0.911" "FC6"    
## [28,] "1.72046147545036"  "0.244" "FC2"    
## [29,] "1.51042269502935"  "0.351" "F4"     
## [30,] "1.56989891786069"  "0.274" "F8"     
## [31,] "1.67188307598539"  "0.236" "Fp2"
sxx <- ggplot(ndf_x, aes(reorder(Electrode, Statistic), Statistic)) +
  geom_col(aes(fill=p.value<0.05)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Electrodes significant in Experiment 1 
       versus Experiment 2 sorted by HD statistic
       for Participant 1") + 
  theme_minimal()

#png('P1E1_P1E2_Electrode_Wise_point_05.#png', units="in", width=5, height=5, res=300)
sxx

#dev.off()

sxx

sxx <- ggplot(ndf_x, aes(reorder(Electrode, Statistic), Statistic)) +
  geom_col(aes(fill=p.value<0.10)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Electrodes significant in Experiment 1 
       versus Experiment 2 sorted by HD statistic
       for Participant 1") + 
  theme_minimal()


#png('P1E1_P1E2_Electrode_Wise_point_1.#png', units="in", width=5, height=5, res=300)
sxx

#dev.off()

sxx

TRIAL WISE E1 E2

first_expt <- eres # all trials electrode wise

l1t <- vector("list", length(first_expt ))

for(i in seq_along(first_expt )){
  for(j in seq_along(first_expt [[i]])){
   l1t[[i]] <-  cov(first_expt [[i]][[j]])
}
}

second_expt <- eres_2 # all trials electrode wise

l2t <- vector("list", length(second_expt)) 
             
for(i in seq_along(second_expt)){
  for(j in seq_along(second_expt[[i]])){
   l2t[[i]] <-  cov(second_expt[[i]][[j]])
}
} 
l1tl2t <- vector("list", length(l1t))

for(i in seq_along(l1t)){
   l1tl2t[[i]] <-  data.table(l1t[i], l2t[i])
}

# perform comparisons

comparisons <- vector("list", length(l1tl2t))

for(i in seq_along(l1tl2t)){
  x <- data.frame(l1tl2t[[i]][["V1"]])
  y <- data.frame(l1tl2t[[i]][["V2"]])
   comparisons[[i]] <- testCov(x, y, method = "HD", J = 1000, alpha = 0.05, n.core = 6)
}


# get names of electrodes
names(comparisons) <- trials$trial_info


# extract electrode names, p values and statistic

cxt <- comparisons

vars <- c("Trial", "Statistics", "P-val")

# extract statistics and p value
location_1 <- lapply(cxt, function (x) x[c("statistics")])
location_2 <- lapply(cxt, function (x) x[c( "p.value")])

#convert statistics into dataframe column
ldf1t <- do.call(rbind, location_1)
ldf1t1 <- do.call(rbind,ldf1t)

#convert pvalues into dataframe column
ldf2t <- do.call(rbind, location_2)

ndf_xt <- data.frame(ldf1t1, ldf2t)

ndf_xt$Trials <- row.names(ndf_xt)

new <- ndf_xt
new <- apply(new,2,as.character)

#write.csv(new,'P1E1_P1E2_Trial_block_Wise.csv', row.names = FALSE)

new
##       Statistic           p.value Trials    
##  [1,] "2.4556291847627"   "0.043" "Trial 1" 
##  [2,] "1.17013619845251"  "0.452" "Trial 2" 
##  [3,] "1.80524626644084"  "0.209" "Trial 3" 
##  [4,] "2.5126723364679"   "0.039" "Trial 4" 
##  [5,] "0.60538508484715"  "0.837" "Trial 5" 
##  [6,] "1.4007146950238"   "0.375" "Trial 6" 
##  [7,] "1.13227229408657"  "0.538" "Trial 7" 
##  [8,] "2.179431526749"    "0.08"  "Trial 8" 
##  [9,] "0.220243843501264" "0.973" "Trial 9" 
## [10,] "0.664312804003264" "0.825" "Trial 10"
## [11,] "0.726211963393938" "0.763" "Trial 11"
## [12,] "1.61009899620245"  "0.261" "Trial 12"
## [13,] "2.3832639061063"   "0.057" "Trial 13"
## [14,] "1.32861156745273"  "0.411" "Trial 14"
## [15,] "0.998341253121267" "0.589" "Trial 15"
## [16,] "1.80875324068461"  "0.162" "Trial 16"
## [17,] "1.27829285933981"  "0.493" "Trial 17"
## [18,] "1.40760171220696"  "0.341" "Trial 18"
## [19,] "1.02521059653791"  "0.603" "Trial 19"
## [20,] "1.67188307598539"  "0.219" "Trial 20"
sxt <- ggplot(ndf_xt, aes(reorder(Trials, Statistic), Statistic))  +
  geom_col(aes(fill=p.value<0.05)) +
  coord_flip() +
  labs(x="Trials", y="Statistic",
       title="Trials significant in Experiment 1 
       versus Experiment 2 sorted by HD statistic
       for Participant 1") + 
  theme_minimal()

#png('P1E1_P1E2_Trial_block_Wise_point_05.#png', units="in", width=5, height=5, res=300)
sxt

#dev.off()

sxt

sxt <- ggplot(ndf_xt, aes(reorder(Trials, Statistic), Statistic))  +
  geom_col(aes(fill=p.value<0.10)) +
  coord_flip() +
  labs(x="Trials", y="Statistic",
       title="Trials significant in Experiment 1 
       versus Experiment 2 sorted by HD statistic
       for Participant 1") + 
  theme_minimal()

#png('P1E1_P1E2_Trail_block_Wise_point_05.#png', units="in", width=5, height=5, res=300)
sxt

#dev.off()

sxt

BETWEEN EXPERIMENTS 1 and 2 for Participant 2

eres <- vector('list', length(split_df3))
  for(i in seq_along(split_df3)){
    for(j in seq_along(split_df3[[i]])){
      eres[[i]][[j]] <- eegfft(split_df3[[i]][[j]], Fs=1000, lower=4, upper=40) # mod of fft
    }
  }

eres_2 <- vector('list', length(split_df4))
  for(i in seq_along(split_df4)){
    for(j in seq_along(split_df4[[i]])){
      eres_2[[i]][[j]] <- eegfft(split_df4[[i]][[j]], Fs=1000, lower=4, upper=40) # mod of fft
    }
  }

ELECTRODE WISE

first_exp <- purrr::transpose(eres) # all trials electrode wise

l1 <- vector("list", length(first_exp ))

for(i in seq_along(first_exp )){
  for(j in seq_along(first_exp [[i]])){
   l1[[i]] <-  cov(first_exp [[i]][[j]])
}
}

second_exp <- purrr::transpose(eres_2) # all trials electrode wise

l2 <- vector("list", length(second_exp)) 
             
for(i in seq_along(second_exp)){
  for(j in seq_along(second_exp[[i]])){
   l2[[i]] <-  cov(second_exp[[i]][[j]])
}
} 
l1l2 <- vector("list", length(l1))

for(i in seq_along(l1)){
   l1l2[[i]] <-  data.table(l1[i], l2[i])
}

# perform comparisons

comparisons <- vector("list", length(l1l2))

for(i in seq_along(l1l2)){
  x <- data.frame(l1l2[[i]][["V1"]])
  y <- data.frame(l1l2[[i]][["V2"]])
   comparisons[[i]] <- testCov(x, y, method = "HD", J = 1000, alpha = 0.05, n.core = 6)
}

# get names of electrodes
names(comparisons) <- col_names

# extract electrode names, p values and statistic

cx <- comparisons

vars <- c("Electrode", "Statistics", "P-val")

# extract statistics and p value
location_1 <- lapply(cx, function (x) x[c("statistics")])
location_2 <- lapply(cx, function (x) x[c( "p.value")])

#convert statistics into dataframe column
ldf1 <- do.call(rbind, location_1)
ldf11 <- do.call(rbind,ldf1)

#convert pvalues into dataframe column
ldf2 <- do.call(rbind, location_2)

ndf_x <- data.frame(ldf11, ldf2)

ndf_x$Electrode <- row.names(ndf_x)

new <- ndf_x
new <- apply(new,2,as.character)

#write.csv(new,'P2E1_P2E2_Electrode_Wise.csv', row.names = FALSE)

new
##       Statistic           p.value Electrode
##  [1,] "1.46984779525798"  "0.322" "Fp1"    
##  [2,] "2.48475730827486"  "0.048" "F3"     
##  [3,] "1.04977472496588"  "0.568" "F7"     
##  [4,] "1.43904406542997"  "0.402" "FT9"    
##  [5,] "1.50722522284865"  "0.329" "FC5"    
##  [6,] "0.85218478997265"  "0.735" "FC1"    
##  [7,] "1.30745602451685"  "0.415" "C3"     
##  [8,] "0.833123703624517" "0.73"  "T7"     
##  [9,] "1.68423790406987"  "0.249" "TP9"    
## [10,] "1.47969941705715"  "0.355" "CP5"    
## [11,] "1.9564318152057"   "0.15"  "CP1"    
## [12,] "1.00548242810256"  "0.61"  "Pz"     
## [13,] "1.3815294679924"   "0.426" "P3"     
## [14,] "0.79426498735262"  "0.769" "P7"     
## [15,] "0.703132100683367" "0.778" "O1"     
## [16,] "1.26074215230192"  "0.44"  "Oz"     
## [17,] "1.92312056422471"  "0.156" "O2"     
## [18,] "1.94135270938834"  "0.171" "P4"     
## [19,] "1.63756270943948"  "0.291" "P8"     
## [20,] "1.76152526158335"  "0.195" "TP10"   
## [21,] "2.00607088410993"  "0.133" "CP6"    
## [22,] "2.90802834726954"  "0.014" "CP2"    
## [23,] "2.07447931233755"  "0.116" "Cz"     
## [24,] "3.02742608024697"  "0.009" "C4"     
## [25,] "2.66553741419556"  "0.021" "T8"     
## [26,] "1.72699140196167"  "0.256" "FT10"   
## [27,] "2.71465496799986"  "0.022" "FC6"    
## [28,] "2.56062176747009"  "0.03"  "FC2"    
## [29,] "2.93016623600207"  "0.008" "F4"     
## [30,] "2.47681843752468"  "0.027" "F8"     
## [31,] "1.92108841703744"  "0.162" "Fp2"
sxx <- ggplot(ndf_x, aes(reorder(Electrode, Statistic), Statistic)) +
  geom_col(aes(fill=p.value<0.05)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Electrodes significant in Experiment 1 
       versus Experiment 2 sorted by HD statistic
       for Participant 2") + 
  theme_minimal()

#png('P2E1_P2E2_Electrode_Wise_point_05.#png', units="in", width=5, height=5, res=300)
sxx

#dev.off()

sxx

sxx <- ggplot(ndf_x, aes(reorder(Electrode, Statistic), Statistic)) +
  geom_col(aes(fill=p.value<0.10)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="Electrodes significant in Experiment 1 
       versus Experiment 2 sorted by HD statistic
       for Participant 2") + 
  theme_minimal()


#png('P2E1_P2E2_Electrode_Wise_point_1.#png', units="in", width=5, height=5, res=300)
sxx

#dev.off()

sxx

TRIAL WISE E1 E2

first_expt <- eres # all trials electrode wise

l1t <- vector("list", length(first_expt ))

for(i in seq_along(first_expt )){
  for(j in seq_along(first_expt [[i]])){
   l1t[[i]] <-  cov(first_expt [[i]][[j]])
}
}

second_expt <- eres_2 # all trials electrode wise

l2t <- vector("list", length(second_expt)) 
             
for(i in seq_along(second_expt)){
  for(j in seq_along(second_expt[[i]])){
   l2t[[i]] <-  cov(second_expt[[i]][[j]])
}
} 
l1tl2t <- vector("list", length(l1t))

for(i in seq_along(l1t)){
   l1tl2t[[i]] <-  data.table(l1t[i], l2t[i])
}

# perform comparisons

comparisons <- vector("list", length(l1tl2t))

for(i in seq_along(l1tl2t)){
  x <- data.frame(l1tl2t[[i]][["V1"]])
  y <- data.frame(l1tl2t[[i]][["V2"]])
   comparisons[[i]] <- testCov(x, y, method = "HD", J = 1000, alpha = 0.05, n.core = 6)
}


# get names of electrodes
names(comparisons) <- trials$trial_info


# extract electrode names, p values and statistic

cxt <- comparisons

vars <- c("Trial", "Statistics", "P-val")

# extract statistics and p value
location_1 <- lapply(cxt, function (x) x[c("statistics")])
location_2 <- lapply(cxt, function (x) x[c( "p.value")])

#convert statistics into dataframe column
ldf1t <- do.call(rbind, location_1)
ldf1t1 <- do.call(rbind,ldf1t)

#convert pvalues into dataframe column
ldf2t <- do.call(rbind, location_2)

ndf_xt <- data.frame(ldf1t1, ldf2t)

ndf_xt$Trials <- row.names(ndf_xt)

new <- ndf_xt
new <- apply(new,2,as.character)

#write.csv(new,'P2E1_P2E2_Trial_block_Wise.csv', row.names = FALSE)

new
##       Statistic           p.value Trials    
##  [1,] "1.90675135922903"  "0.195" "Trial 1" 
##  [2,] "0.311067294966483" "0.946" "Trial 2" 
##  [3,] "0.839597089985581" "0.675" "Trial 3" 
##  [4,] "1.49629197165541"  "0.324" "Trial 4" 
##  [5,] "1.61563988943544"  "0.266" "Trial 5" 
##  [6,] "0.565153651412109" "0.826" "Trial 6" 
##  [7,] "1.98340697609228"  "0.127" "Trial 7" 
##  [8,] "1.76719240791527"  "0.217" "Trial 8" 
##  [9,] "1.39602843318014"  "0.368" "Trial 9" 
## [10,] "0.91880384224504"  "0.647" "Trial 10"
## [11,] "1.061582328777"    "0.557" "Trial 11"
## [12,] "2.11437984941205"  "0.123" "Trial 12"
## [13,] "1.05355390805172"  "0.621" "Trial 13"
## [14,] "1.0081857772576"   "0.612" "Trial 14"
## [15,] "0.585737278786239" "0.884" "Trial 15"
## [16,] "2.04996962262185"  "0.087" "Trial 16"
## [17,] "1.77842418483841"  "0.176" "Trial 17"
## [18,] "2.38093659595115"  "0.053" "Trial 18"
## [19,] "2.28634767601238"  "0.058" "Trial 19"
## [20,] "1.92108841703744"  "0.129" "Trial 20"
sxt <- ggplot(ndf_xt, aes(reorder(Trials, Statistic), Statistic))  +
  geom_col(aes(fill=p.value<0.05)) +
  coord_flip() +
  labs(x="Trials", y="Statistic",
       title="Trials significant in Experiment 1 
       versus Experiment 2 sorted by HD statistic
       for Participant 2") + 
  theme_minimal()

#png('P2E1_P2E2_Trial_block_Wise_point_05.#png', units="in", width=5, height=5, res=300)
sxt

#dev.off()

sxt

sxt <- ggplot(ndf_xt, aes(reorder(Trials, Statistic), Statistic))  +
  geom_col(aes(fill=p.value<0.10)) +
  coord_flip() +
  labs(x="Trials", y="Statistic",
       title="Trials significant in Experiment 1 
       versus Experiment 2 sorted by HD statistic
       for Participant 2") + 
  theme_minimal()

#png('P2E1_P2E2_Trail_block_Wise_point_05.#png', units="in", width=5, height=5, res=300)
sxt

#dev.off()

sxt

Between P1 and P2

c_df1 <- rbind(split_df1, split_df2)

c_df2 <- rbind(split_df3, split_df4)

ceres <- vector('list', length(c_df1))
  for(i in seq_along(c_df1)){
    for(j in seq_along(c_df1[[i]])){
      ceres[[i]][[j]] <- eegfft(c_df1[[i]][[j]], Fs=1000, lower=4, upper=40) # mod of fft
    }
  }

ceres_2 <- vector('list', length(c_df2))
  for(i in seq_along(c_df2)){
    for(j in seq_along(c_df2[[i]])){
      ceres_2[[i]][[j]] <- eegfft(c_df2[[i]][[j]], Fs=1000, lower=4, upper=40) # mod of fft
    }
  }

ELECTRODE WISE

first_participant <- purrr::transpose(ceres) # all trials electrode wise

cl1 <- vector("list", length(first_participant ))

for(i in seq_along(first_participant )){
  for(j in seq_along(first_participant [[i]])){
   cl1[[i]] <-  cov(first_participant [[i]][[j]])
}
}

second_participant <- purrr::transpose(ceres_2) # all trials electrode wise

cl2 <- vector("list", length(second_participant)) 
             
for(i in seq_along(second_participant)){
  for(j in seq_along(second_participant[[i]])){
   cl2[[i]] <-  cov(second_participant[[i]][[j]])
}
} 
cl1cl2 <- vector("list", length(cl1))

for(i in seq_along(cl1)){
   cl1cl2[[i]] <-  data.table(cl1[i], cl2[i])
}

# perform comparisons

comparisons <- vector("list", length(cl1cl2))

for(i in seq_along(cl1cl2)){
  x <- data.frame(cl1cl2[[i]][["V1"]])
  y <- data.frame(cl1cl2[[i]][["V2"]])
   comparisons[[i]] <- testCov(x, y, method = "HD", J = 1000, alpha = 0.05, n.core = 6)
}

# get names of electrodes
names(comparisons) <- col_names

# extract electrode names, p values and statistic

ccx <- comparisons

vars <- c("Electrode", "Statistics", "P-val")

# extract statistics and p value
location_1 <- lapply(ccx, function (x) x[c("statistics")])
location_2 <- lapply(ccx, function (x) x[c( "p.value")])

#convert statistics into dataframe column
cldf1 <- do.call(rbind, location_1)
cldf11 <- do.call(rbind,cldf1)

#convert pvalues into dataframe column
cldf2 <- do.call(rbind, location_2)

cndf_x <- data.frame(cldf11, cldf2)

cndf_x$Electrode <- col_names

ccnew <- cndf_x

ccnew <- apply(ccnew,2,as.character)

#write.csv(ccnew,'P1_P2_Electrode_Wise.csv', row.names = FALSE)

ccnew
##       Statistic           p.value Electrode
##  [1,] "2.26547445824321"  "0.08"  "Fp1"    
##  [2,] "1.16277874396881"  "0.571" "F3"     
##  [3,] "1.3947561311045"   "0.37"  "F7"     
##  [4,] "2.17427946652384"  "0.102" "FT9"    
##  [5,] "1.74508297212358"  "0.226" "FC5"    
##  [6,] "1.17319901246185"  "0.514" "FC1"    
##  [7,] "2.07146224901742"  "0.127" "C3"     
##  [8,] "1.35387668594565"  "0.445" "T7"     
##  [9,] "0.646494888266546" "0.827" "TP9"    
## [10,] "1.41401456783423"  "0.416" "CP5"    
## [11,] "2.0844870346724"   "0.101" "CP1"    
## [12,] "1.96781763116702"  "0.158" "Pz"     
## [13,] "1.33096990464358"  "0.432" "P3"     
## [14,] "1.36178188971377"  "0.41"  "P7"     
## [15,] "1.6229760710228"   "0.239" "O1"     
## [16,] "0.538035164287667" "0.863" "Oz"     
## [17,] "1.16746790284816"  "0.506" "O2"     
## [18,] "1.18080663807392"  "0.489" "P4"     
## [19,] "1.57659136183868"  "0.302" "P8"     
## [20,] "1.25750688742798"  "0.442" "TP10"   
## [21,] "2.52450977999619"  "0.038" "CP6"    
## [22,] "1.67172663264323"  "0.256" "CP2"    
## [23,] "2.12708154219824"  "0.096" "Cz"     
## [24,] "2.5195612323265"   "0.051" "C4"     
## [25,] "0.977407034740267" "0.631" "T8"     
## [26,] "1.41989350342319"  "0.366" "FT10"   
## [27,] "1.73162003974933"  "0.218" "FC6"    
## [28,] "2.20411547641786"  "0.104" "FC2"    
## [29,] "1.65155637744611"  "0.275" "F4"     
## [30,] "1.76017325584036"  "0.207" "F8"     
## [31,] "0.589193426074097" "0.852" "Fp2"
csxx <- ggplot(cndf_x, aes(reorder(Electrode, Statistic), Statistic)) +
  geom_col(aes(fill=p.value<0.05)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="P1 versus P2") + 
  theme_minimal()

#png('P1P2_Electrode_Wise_point_05.#png', units="in", width=5, height=5, res=300)
csxx

#dev.off()

csxx

csxx <- ggplot(cndf_x, aes(reorder(Electrode, Statistic), Statistic)) +
  geom_col(aes(fill=p.value<0.10)) +
  coord_flip() +
  labs(x="Electrode", y="Statistic",
       title="P1 versus P2") + 
  theme_minimal()


#png('P1P2_Electrode_Wise_point_1.#png', units="in", width=5, height=5, res=300)
csxx

#dev.off()

csxx

TRIAL WISE E1 E2

first_participant_t <- ceres # all trials electrode wise

ccl1t <- vector("list", length(first_participant_t ))

for(i in seq_along(first_participant_t )){
  for(j in seq_along(first_participant_t [[i]])){
   ccl1t[[i]] <-  cov(first_participant_t [[i]][[j]])
}
}

second_participant_t <- ceres_2 # all trials electrode wise

ccl2t <- vector("list", length(second_participant_t)) 
             
for(i in seq_along(second_participant_t)){
  for(j in seq_along(second_participant_t[[i]])){
   ccl2t[[i]] <-  cov(second_participant_t[[i]][[j]])
}
} 
ccl1tccl2t <- vector("list", length(ccl1t))

for(i in seq_along(ccl1t)){
   ccl1tccl2t[[i]] <-  data.table(ccl1t[i], ccl2t[i])
}

# perform comparisons

comparisons <- vector("list", length(ccl1tccl2t))

for(i in seq_along(ccl1tccl2t)){
  x <- data.frame(ccl1tccl2t[[i]][["V1"]])
  y <- data.frame(ccl1tccl2t[[i]][["V2"]])
   comparisons[[i]] <- testCov(x, y, method = "HD", J = 1000, alpha = 0.05, n.core = 6)
}


# get names of electrodes
names(comparisons) <- trials$trial_info


# extract electrode names, p values and statistic

cccxt <- comparisons

vars <- c("Trial", "Statistics", "P-val")

# extract statistics and p value
location_1 <- lapply(cccxt, function (x) x[c("statistics")])
location_2 <- lapply(cccxt, function (x) x[c( "p.value")])

#convert statistics into dataframe column
ccldf1t <- do.call(rbind, location_1)
ccldf1t1 <- do.call(rbind,ccldf1t)

#convert pvalues into dataframe column
ccldf2t <- do.call(rbind, location_2)

ccndf_xt <- data.frame(ccldf1t1, ccldf2t)

ccndf_xt$Trials <- row.names(ccndf_xt)

ccnew <- ccndf_xt
ccnew <- apply(ccnew,2,as.character)

#write.csv(ccnew,'P1P2_Trial_block_Wise.csv', row.names = FALSE)

ccnew
##       Statistic           p.value Trials    
##  [1,] "1.45903296483048"  "0.356" "Trial.1" 
##  [2,] "2.51565566371668"  "0.049" "Trial.2" 
##  [3,] "0.800376722822547" "0.768" "Trial.3" 
##  [4,] "1.19819924603095"  "0.463" "Trial.4" 
##  [5,] "1.16826619520635"  "0.547" "Trial.5" 
##  [6,] "2.4570077862769"   "0.048" "Trial.6" 
##  [7,] "2.30668529143655"  "0.073" "Trial.7" 
##  [8,] "1.6540482680117"   "0.234" "Trial.8" 
##  [9,] "0.513757941217959" "0.853" "Trial.9" 
## [10,] "1.78855263047733"  "0.206" "Trial.10"
## [11,] "1.00193720428146"  "0.578" "Trial.11"
## [12,] "1.80038160329076"  "0.193" "Trial.12"
## [13,] "0.804088711113152" "0.745" "Trial.13"
## [14,] "1.45993734869708"  "0.344" "Trial.14"
## [15,] "1.66815817666184"  "0.262" "Trial.15"
## [16,] "2.23011213250772"  "0.074" "Trial.16"
## [17,] "1.21830153258215"  "0.449" "Trial.17"
## [18,] "2.18182786006891"  "0.077" "Trial.18"
## [19,] "1.00832405848261"  "0.555" "Trial.19"
## [20,] "1.09726371915357"  "0.552" "Trial.20"
## [21,] "0.872423801797757" "0.681" "NA."     
## [22,] "1.56475545947068"  "0.279" "NA..1"   
## [23,] "1.8338799361424"   "0.171" "NA..2"   
## [24,] "1.92096496397629"  "0.165" "NA..3"   
## [25,] "1.24005707677406"  "0.417" "NA..4"   
## [26,] "1.21764151991329"  "0.512" "NA..5"   
## [27,] "0.347296974108352" "0.935" "NA..6"   
## [28,] "1.74694790417823"  "0.232" "NA..7"   
## [29,] "1.55766677793008"  "0.322" "NA..8"   
## [30,] "1.19326411968178"  "0.51"  "NA..9"   
## [31,] "1.27790965327849"  "0.463" "NA..10"  
## [32,] "0.639384852618683" "0.764" "NA..11"  
## [33,] "1.8729130408378"   "0.17"  "NA..12"  
## [34,] "0.622340795277693" "0.838" "NA..13"  
## [35,] "2.1197006910622"   "0.088" "NA..14"  
## [36,] "0.901431702143973" "0.69"  "NA..15"  
## [37,] "0.868544542329733" "0.675" "NA..16"  
## [38,] "1.76616110195516"  "0.199" "NA..17"  
## [39,] "0.739836055569746" "0.758" "NA..18"  
## [40,] "0.589193426074097" "0.856" "NA..19"
sxt <- ggplot(ccndf_xt, aes(reorder(Trials, Statistic), Statistic))  +
  geom_col(aes(fill=p.value<0.05)) +
  coord_flip() +
  labs(x="Trials", y="Statistic",
       title="Trials significant in Experiment 1 
       versus Experiment 2 sorted by HD statistic
       for P1 versus P2") + 
  theme_minimal()

#png('P1_P2_Trial_block_Wise_point_05.#png', units="in", width=5, height=5, res=300)
sxt

#dev.off()

sxt

sxt <- ggplot(ccndf_xt, aes(reorder(Trials, Statistic), Statistic))  +
  geom_col(aes(fill=p.value<0.10)) +
  coord_flip() +
  labs(x="Trials", y="Statistic",
       title="Trials significant in Experiment 1 
       versus Experiment 2 sorted by HD statistic
       for P1 versus P2") + 
  theme_minimal()

#png('P1_P2_Trail_block_Wise_point_05.#png', units="in", width=5, height=5, res=300)
sxt

#dev.off()

sxt

Total comparisons = 7

Electrodes S1 S2 P1E1
Trials S1 S2 P1E1

Electrodes S1 S2 P1E2
Trials S1 S2 P1E2

Electrodes S1 S2 P2E1
Trials S1 S2 P1E1

Electrodes S1 S2 P2E2
Trials S1 S2 P1E2







Electrodes P1E1 P1E2
Trials P1E1 P2E2

Electrodes P2E1 P2E2
Trials P2E1 P2E2







Electrodes P1 P2
Trials P1 P2